package com.mysterlee.test;

import java.net.*;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class TcpIpServer{
	@SuppressWarnings("resource")
	
	public static void main(String args[]){
		ServerSocket serverSocket = null;
		
		try{
			serverSocket = new ServerSocket(7777);
			System.out.println(getTime()+"서버가 준비됨");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		while(true){
			try{
				System.out.println(getTime()+"연결 요청중");
				Socket socket = serverSocket.accept();
				System.out.println(getTime()+socket.getInetAddress()+"로 부터 연결요청");
				
				OutputStream out = socket.getOutputStream();
				DataOutputStream dos = new DataOutputStream(out);
				
				dos.writeUTF("[Notice] Test");
				System.out.println(getTime()+"데이터를 전송");
				
				dos.close();
				socket.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	static String getTime(){
		SimpleDateFormat f = new SimpleDateFormat("[hh:mm:ss]");
		return f.format(new Date());
	}
}